package com.thinta.wind.ui.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.thinta.wind.R
import com.thinta.wind.base.BasePagedListAdapterObserver
import com.thinta.wind.databinding.DailyContentItemViewBinding
import com.thinta.wind.databinding.DailyFooterViewBinding
import com.thinta.wind.entity.HomeDailyItemListBean
import com.thinta.wind.interfaces.OnItemClickListener

/*********************************************************
 * 版权信息：2021/6/7 16:54北京振中电子技术有限公司版权所有<p>
 * 创建作者：Wind<p>
 * 创建日期：2021/6/7 16:54<p>
 * *******************************************************<p>
 * 更新历史：    作者      日期      更新摘要<p>
 * <p>
 * *******************************************************<p>
 * 类功能说明：<p>
 */
class DailyAdapter : PagedListAdapter<HomeDailyItemListBean, RecyclerView.ViewHolder>(diffCallback){

    var isLoadMore = 0

    private lateinit var itemListener: OnItemClickListener

    fun setOnItemClickListener(itemListener: OnItemClickListener){
        this.itemListener = itemListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
        when (viewType) {
            ITEM_TYPE_FOOTER -> FooterViewHolder(parent)
            else -> DailyViewHolder(parent)
        }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is DailyViewHolder -> getItem(position)?.let { holder.bindTo(it, itemListener) }
            is FooterViewHolder -> holder.bindFooter(isLoadMore)
        }
    }

    override fun getItemViewType(position: Int): Int =
        when (position) {
            itemCount - 1 -> ITEM_TYPE_FOOTER
            else -> super.getItemViewType(position)
        }

    override fun getItemCount(): Int = super.getItemCount() + 1

    override fun registerAdapterDataObserver(observer: RecyclerView.AdapterDataObserver) {
        super.registerAdapterDataObserver(BasePagedListAdapterObserver(observer, 1))
    }

    companion object {
        private val diffCallback = object : DiffUtil.ItemCallback<HomeDailyItemListBean>() {
            override fun areItemsTheSame(oldItem: HomeDailyItemListBean, newItem: HomeDailyItemListBean): Boolean =
                oldItem.id == newItem.id
            override fun areContentsTheSame(oldItem: HomeDailyItemListBean, newItem: HomeDailyItemListBean): Boolean =
                oldItem == newItem
        }
        private const val ITEM_TYPE_FOOTER = 100
    }
}

class DailyViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(
    R.layout.daily_content_item_view, parent, false)){

    private lateinit var dailyContentItemViewBinding : DailyContentItemViewBinding

    fun bindTo(data: HomeDailyItemListBean, listener: OnItemClickListener){
        dailyContentItemViewBinding = initViewBindingImpl(itemView) as DailyContentItemViewBinding
        if(data.type == "textCard"){
            dailyContentItemViewBinding.dailyLayout.layoutParams.height = 0
        }else{
            dailyContentItemViewBinding.dailyLayout.layoutParams.height = CoordinatorLayout.LayoutParams.WRAP_CONTENT
            data.data.header.iconType = dailyContentItemViewBinding.root.resources.getString(R.string.publish)+"："
            data.data.header.issuerName = data.data.content.data.author.name
            data.data.header.iconType = data.data.content.data.author.description
        }
        dailyContentItemViewBinding.item = data
        dailyContentItemViewBinding.dailyLayout.setOnClickListener {
            listener.onItemClick(layoutPosition, it)
        }
    }
}

class FooterViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(
    R.layout.daily_footer_view, parent, false)){

    private lateinit var mFooterBinding : DailyFooterViewBinding

    fun bindFooter(isLoadMore : Int){
        mFooterBinding = initViewBindingImpl(itemView) as DailyFooterViewBinding
        when(isLoadMore){
            0 ->  mFooterBinding.footerContent.text = ""
            1 ->  mFooterBinding.footerContent.text = mFooterBinding.root.resources.getString(R.string.loading)
            -1 ->  mFooterBinding.footerContent.text = mFooterBinding.root.resources.getString(R.string.not_more)
        }
    }
}

private fun initViewBindingImpl(itemView: View): ViewDataBinding? =
    if (null == DataBindingUtil.getBinding(itemView)) {
        DataBindingUtil.bind(itemView)
    } else {
        DataBindingUtil.getBinding(itemView)
    }